package org.boyan.modules.cgreport.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.boyan.common.system.query.QueryGenerator;
import org.boyan.common.util.DateUtils;
import org.boyan.common.util.oConvertUtils;
import org.boyan.modules.cgform.util.b;
import org.boyan.modules.cgreport.entity.OnlCgreportItem;
import org.boyan.modules.config.b.d;
import org.boyan.modules.config.exception.DBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author: zhuxiaobao
 * @time: 2021/9/26 10:13
 */

public class a {
    private static final Logger a = LoggerFactory.getLogger(a.class);

    public static void a(HttpServletRequest paramHttpServletRequest, Map<String, Object> paramMap1, Map<String, Object> paramMap2, Map<String, Object> paramMap3) {
        String str1 = (String)paramMap1.get("field_name");
        String str2 = (String)paramMap1.get("search_mode");
        String str3 = (String)paramMap1.get("field_type");
        if ("single".equals(str2)) {
            String str = paramHttpServletRequest.getParameter(str1.toLowerCase());
            try {
                if (oConvertUtils.isEmpty(str)) {
                    return;
                }
                String str4 = paramHttpServletRequest.getQueryString();
                if (str4.contains(str1 + "=")) {
                    String str5 = new String(str.getBytes("ISO-8859-1"), "UTF-8");
                    str = str5;
                }
            } catch (UnsupportedEncodingException unsupportedEncodingException) {
                a.error(unsupportedEncodingException.getMessage(), unsupportedEncodingException);
                return;
            }
            if (oConvertUtils.isNotEmpty(str))
                if (str.contains("*")) {
                    str = str.replaceAll("\\*", "%");
                    paramMap2.put(str1, " LIKE :" + str1);
                } else {
                    paramMap2.put(str1, " = :" + str1);
                }
            paramMap3.put(str1, a(str3, str, true));
        } else if ("group".equals(str2)) {
            String str4 = paramHttpServletRequest.getParameter(str1.toLowerCase() + "_begin");
            String str5 = paramHttpServletRequest.getParameter(str1.toLowerCase() + "_end");
            if (oConvertUtils.isNotEmpty(str4)) {
                String str = " >= :" + str1 + "_begin";
                paramMap2.put(str1, str);
                paramMap3.put(str1 + "_begin", a(str3, str4, true));
            }
            if (oConvertUtils.isNotEmpty(str5)) {
                String str = " <= :" + str1 + "_end";
                paramMap2.put(new String(str1), str);
                paramMap3.put(str1 + "_end", a(str3, str5, false));
            }
        }
    }

    private static Object a(String paramString1, String paramString2, boolean paramBoolean) {
        String str = null;
        if (oConvertUtils.isNotEmpty(paramString2)) {
            if ("String".equalsIgnoreCase(paramString1)) {
                str = paramString2;
            } else if ("Date".equalsIgnoreCase(paramString1)) {
                if (paramString2.length() != 19 &&
                        paramString2.length() == 10)
                    if (paramBoolean) {
                        paramString2 = paramString2 + " 00:00:00";
                    } else {
                        paramString2 = paramString2 + " 23:59:59";
                    }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = DateUtils.str2Date(paramString2, simpleDateFormat);
            } else if ("Double".equalsIgnoreCase(paramString1)) {
                str = paramString2;
            } else if ("Integer".equalsIgnoreCase(paramString1)) {
                str = paramString2;
            } else {
                str = paramString2;
            }
        }
        return str;
    }

    public static String a(List<Map<String, Object>> paramList, Long paramLong) {
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        jSONObject.put("total", paramLong);
        if (paramList != null) {
            for (Map<String, Object> map : paramList) {
                JSONObject jSONObject1 = new JSONObject();
                Iterator<String> iterator = map.keySet().iterator();
                while (iterator.hasNext()) {
                    String str1 = iterator.next();
                    String str2 = String.valueOf(map.get(str1));
                    str1 = str1.toLowerCase();
                    if (str1.contains("time") || str1.contains("date")) {
                        str2 = a(str2);
                    }
                    jSONObject1.put(str1, str2);
                }
                jSONArray.add(jSONObject1);
            }
        }
        jSONObject.put("rows", jSONArray);
        return jSONObject.toString();
    }

    public static String a(List<Map<String, Object>> paramList) {
        JSONArray jSONArray = new JSONArray();
        for (Map<String, Object> map : paramList) {
            JSONObject jSONObject = new JSONObject();
            for (String str1 : map.keySet()) {
                String str2 = String.valueOf(map.get(str1));
                str1 = str1.toLowerCase();
                if (str1.contains("time") || str1.contains("date")) {
                    str2 = a(str2);
                }
                jSONObject.put(str1, str2);
            }
            jSONArray.add(jSONObject);
        }
        return jSONArray.toString();
    }

    public static String a(String paramString) {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat1.parse(paramString);
            return simpleDateFormat2.format(date);
        } catch (Exception exception) {
            return paramString;
        }
    }

    public static String a(List<OnlCgreportItem> paramList, Map<String, Object> paramMap, String paramString1, String paramString2) {
        StringBuffer stringBuffer = new StringBuffer();
        String str = paramString2;
        if (str == null) {
            try {
                str = d.getDatabaseType();
            } catch (SQLException sQLException) {
                sQLException.printStackTrace();
            } catch (DBException dBException) {
                dBException.printStackTrace();
            }
        }
        HashSet<String> hashSet = new HashSet();
        for (OnlCgreportItem onlCgreportItem : paramList) {
            String str1 = onlCgreportItem.getFieldName();
            String str2 = onlCgreportItem.getFieldType();
            if (1 == onlCgreportItem.getIsSearch()) {
                if ("group".equals(onlCgreportItem.getSearchMode())) {
                    Object object1 = paramMap.get(str1 + "_begin");
                    if (object1 != null) {
                        stringBuffer.append(" and " + paramString1 + str1 + " >= ");
                        if ("Long".equals(str2) || "Integer".equals(str2)) {
                            stringBuffer.append(object1.toString());
                        } else if ("ORACLE".equals(str)) {
                            if (str2.toLowerCase().equals("datetime")) {
                                stringBuffer.append(b.a(object1.toString()));
                            } else if (str2.toLowerCase().equals("date")) {
                                stringBuffer.append(b.b(object1.toString()));
                            }
                        } else {
                            stringBuffer.append("'" + object1.toString() + "'");
                        }
                    }
                    Object object2 = paramMap.get(str1 + "_end");
                    if (object2 != null) {
                        stringBuffer.append(" and " + paramString1 + str1 + " <= ");
                        if ("Long".equals(str2) || "Integer".equals(str2)) {
                            stringBuffer.append(object2.toString());
                            continue;
                        }
                        if ("ORACLE".equals(str)) {
                            if (str2.toLowerCase().equals("datetime")) {
                                stringBuffer.append(b.a(object2.toString()));
                                continue;
                            }
                            if (str2.toLowerCase().equals("date"))
                                stringBuffer.append(b.b(object2.toString()));
                            continue;
                        }
                        stringBuffer.append("'" + object2.toString() + "'");
                    }
                    continue;
                }
                Object object = paramMap.get(str1);
                if (object != null) {
                    boolean bool = (!"Long".equals(str2) && !"Integer".equals(str2)) ? true : false;
                    if ("ORACLE".equals(str)) {
                        if (str2.toLowerCase().equals("datetime")) {
                            object = b.a(object.toString());
                            bool = false;
                        } else if (str2.toLowerCase().equals("date")) {
                            object = b.b(object.toString());
                            bool = false;
                        }
                    }
                    String str3 = QueryGenerator.getSingleQueryConditionSql(str1, paramString1, object, bool, str);
                    stringBuffer.append(" and " + str3);
                    hashSet.add(str1);
                }
            }
        }
        for (String str1 : paramMap.keySet()) {
            if (str1.startsWith("popup_param_pre__")) {
                String str2 = paramMap.get(str1).toString();
                String str3 = str1.substring("popup_param_pre__".length());
                if (hashSet.contains(str3)) {
                    continue;
                }
                String str4 = QueryGenerator.getSingleQueryConditionSql(str3, paramString1, str2, false);
                stringBuffer.append(" and " + str4);
            }
        }
        return stringBuffer.toString();
    }
}
